package com.wdy.entities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

@Data
public class SecurityUser implements UserDetails {

  private transient User currentUserInfo;
  private List<String> permissionValueList;

  public SecurityUser() {
  }

  public SecurityUser(User user) {
    if (null != user) {
      this.currentUserInfo = user;
    }
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    Collection<GrantedAuthority> authorities = new ArrayList<>();
    for (String permissionValue : permissionValueList) {
      if (StringUtils.isEmpty(permissionValue)) {
        continue;
      }

      SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permissionValue);
      authorities.add(authority);
    }
    return authorities;
  }

  @Override
  public String getPassword() {
    return this.currentUserInfo.getPassword();
  }

  @Override
  public String getUsername() {
    return this.currentUserInfo.getUsername();
  }

  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  public boolean isEnabled() {
    return true;
  }
}
